<template>
  <el-container class="conbox">
    <el-header>
      <div>
        <img class="imglogo" src="../assets/logo.png" />
        <span class="spanlogo">控制台</span>
      </div>

      <el-button @click="logout" type="info"> 退出</el-button>
    </el-header>

    <el-container>
      <!-- //侧边栏 -->

      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="home_logn">
          <span class="el-icon-s-tools" @click="hone_login"></span>
        </div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="red"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ' '"
            v-for="item in muenuslist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconobj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item-group>
              <el-menu-item
                :index="'/' + subitem.path"
                v-for="subitem in item.children"
                :key="subitem.id"
                @click="saveindexsaty('/' + subitem.path)"
              >
                <i class="el-icon-menu">{{ subitem.authName }}</i>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右边栏 -->
      <el-main>
        <router-view> </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  //生命周期函数
  //实例在被创建（刷新）之前都要经过一系列的初始化过程
  //执行自定义的方法
  data() {
    return {
      muenuslist: [],
      iconobj: {
        125: "el-icon-user-solid",
        103: "el-icon-view",
        101: "el-icon-s-goods",
        102: "el-icon-s-order",
        145: "el-icon-loading",
      },
      //是否折叠
      isCollapse: false,
      //激活连接地址
      activePath: "",
    };
  },
  created() {
    this.getMenusLists();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenusLists() {
      const { data: res } = await this.$http.get("menus");

      if (res.meta.status != 200) {
        return this.$message.error("error: " + res.meta.msg);
      } else {
        this.$message({
          message: "success:" + res.meta.msg,
          type: "success",
        });
        //数据获取
        this.muenuslist = res.data;
        // console.log("22", res.data);
      }
    },
    hone_login() {
      this.isCollapse = !this.isCollapse;
    },
    saveindexsaty(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: rgb(98, 98, 216);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border-right: none;
  }
}
.conbox {
  height: 100%;
}
.imglogo {
  width: 40px;
  margin-top: 10px;
}
.spanlogo {
  margin-left: 10px;
  position: absolute;
  top: 20px;
}
.home_logn {
  text-align: center;
  font-size: 20px;
  line-height: 10px;
  margin-top: 5px;
  margin-bottom: 5px;
  cursor: pointer;
}
</style>

